package com.leyou.item.controller;

/**
 * 商品服务控制层
 */
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
//@CrossOrigin({"http://manage.leyou.com","*"})  //不建议使用该方法解决跨域问题, 不便于维护
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 根据分类的父id查询分类列表
     * 如果当前处理器要被其他服务通过feign调用。
     * 那么简单类型的参数必须加@RequestParam
     */
    @GetMapping("/category/of/parent")
    public ResponseEntity<List<Category>> findCategoryByPid(@RequestParam("pid") Long pid){
        List<Category> list = categoryService.findCategoryByPid(pid);
//        return ResponseEntity.status(HttpStatus.OK).body(list);
        return ResponseEntity.ok(list); //简写,与上面效果相同
    }


    /*根据分类id的集合查询分类对象的集合*/
    @GetMapping("/category/list")
    public ResponseEntity<List<Category>> findCategorysByIds(@RequestParam("ids") List<Long> ids){

        List<Category> categoryList = categoryService.findCategorysByIds(ids);
        return ResponseEntity.ok(categoryList);

    }

    /*批量查询一级分类信息*/
    @GetMapping("/category/listBatch")
    public ResponseEntity<List<CategoryDTO>> findCategorys(){
        List<CategoryDTO> categoryList = categoryService.findCategorys();
        return ResponseEntity.ok(categoryList);
    }

    /*根据一级分类查询所有子分类*/
    @GetMapping("/category/listChild")
    public ResponseEntity<Map<String, List<String>>> findChildCategory(Long categoryId){
        Map<String, List<String>> categoryMap = categoryService.findChildCategory(categoryId);
        return ResponseEntity.ok(categoryMap);
    }
}
